package com.zrrd.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author WanJl
 * @version 1.0
 * @title DBUtils
 * @description
 * @create 2024/1/22 16:34
 */
public class DBUtils {
    private static SqlSessionFactory sqlSessionFactory;
    static {
        String resources="mybatis-config.xml";
        try {
            InputStream inputStream = Resources.getResourceAsStream(resources);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    public static SqlSessionFactory getSessionFactory(){
        return sqlSessionFactory;
    }


    public static SqlSession getSession(){
        return getSessionFactory().openSession();
    }


    public static SqlSession getSession(boolean autocommit){
        return getSessionFactory().openSession(autocommit);
    }


}
